#include "../Header/Precompiled.h"
#include "../Header/Main.h"
//bool ison = true;

const char windowTitle[] = "Zeldor"; // title displayed on application window
const int ClientWidth = 512; const int ClientHeight = 512; // screen dimensions

// ---------------------------------------------------------------------------

void EnableMemoryLeakChecking(int breakAlloc=-1)
{
	//Set the leak checking flag
	int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(tmpDbgFlag);
	//If a valid break alloc provided set the breakAlloc
	if(breakAlloc!=-1) _CrtSetBreakAlloc( breakAlloc );
}

// ---------------------------------------------------------------------------
void lonk(Graphics* graphics);

int WINAPI WinMain(HINSTANCE instanceH, HINSTANCE prevInstanceH, LPSTR command_line, int show)
{
	EnableMemoryLeakChecking();
	GameStateManager* manager = new GameStateManager(2); // create the game state manager
	WindowsSystem* windows = new WindowsSystem(windowTitle, ClientWidth, ClientHeight);
	
	Graphics* graphics = new Graphics(windows->hWnd, ClientWidth, ClientHeight);
	Game * game = new Game();

	GameObject* lonk = new GameObject(game);

	GameObject* monster = new GameObject(game);

	Input * input = new Input(lonk);

	game->addGameObject(lonk);

	game->addGameObject(monster);

	Vec2 lonkPos;
	lonkPos.x = 1;
	lonkPos.y = 1;
	
	Vec2 lonkDim;
	lonkDim.x = 17;
	lonkDim.y = 24;

	Vec2 monsterPos;
	monsterPos.x = 100;
	monsterPos.y = 100;

	lonk->setPosition(lonkPos);
	lonk->setDimension(lonkDim);

	monster->setPosition(monsterPos);
	monster->setDimension(lonkDim);

	lonk->addComponent(graphics->AddSprites("assets/lonk.png"));

	monster->addComponent(graphics->AddSprites("assets/obstacle.png"));

	Movable* lonkMovable = new Movable(lonk);

	Movable* monsterMovable = new Movable(monster);

	lonk->addComponent(lonkMovable);

	monster->addComponent(monsterMovable);

	lonk->setSolid(true);
	monster->setSolid(true);

	windows->ActivateWindow(); // show the window
	// create systems and add to game state manager
	manager->AddSystem(windows);
	manager->AddSystem(graphics);
	manager->AddSystem(input);
	// Load
	manager->RunLoop(); // enter game loop
	// Init
	manager->Init();
	// Update loop
	manager->Update();
	// Free
	manager->Free();
	// Unload
	manager->Unload();
	// Delete 
	delete manager;
	return 0;
}

//A basic error output function
bool SignalErrorHandler(const char * exp, const char * file, int line, const char * msg , ...)
{
	const int BufferSize = 1024;
	char FinalMessage[BufferSize];

	//Print out the file and line in visual studio format so the error can be
	//double clicked in the output window file(line) : error
	int offset = sprintf_s(FinalMessage,"%s(%d) : ", file , line );	
	if (msg != NULL)
	{	
		va_list args;
		va_start(args, msg);
		vsnprintf_s(FinalMessage + offset, BufferSize - offset, _TRUNCATE , msg, args);
		va_end(args);
	}
	else
	{
		strcpy_s(FinalMessage + offset, BufferSize - offset, "No Error Message");
	}

	//Print to visual studio output window
	OutputDebugString(FinalMessage);
	OutputDebugString("\n");

	//Display a message box to the user
	MessageBoxA(NULL, FinalMessage, "Error", 0);
	
	//Do not debug break
	return true;
}

// ---------------------------------------------------------------------------